// 获取应用实例
const app = getApp()
const api = app.globalData.api


Page({
    data: {
        info: [],
        detail: {},
        targetOption: [],
        typeOption: [],
        calendar: false,
        nowDate: new Date().getTime(),
        popup: false,
        typePopup: false,
        total: 0,
        count: 0
    },
    // 获取往来单位
    getTarget: async function () {
        let res = await api.curlGet('?cla=inventoryOther&fun=getTarget')
        if (res.code === 1) {
            this.setData({
              targetOption: res.data
            })
        }
    },
    // 获取往来单位
    getTypeOption: async function () {
      let res = await api.curlGet('?cla=outStock&fun=search')
      if (res.code === 1) {
          this.setData({
            typeOption: res.data.source
          })
      }
    },
    main: async function () {
        await this.getTarget()
        await this.getTypeOption()
    },
    // 更新本地数据
    syncStorage: async function (id) {
        let that = this
        let res = await api.curlPost('?cla=outStock&fun=detailOther', {id: id})
        if (res.code === 1) {
            let outStockData = res.data.result.detail
            wx.setStorageSync('outStockInfo', res.data.result.info)
            wx.setStorageSync('outStockData', outStockData)
            that.setData({
                total: api.total(outStockData, ['price', 'num']),
                count: api.count(outStockData),
                info: res.data.result.info,
                detail: outStockData
            })
        }
    },
    onLoad: async function (options) {
        let that = this,
            id = options.id ? options.id : ''
        if (id) {
            await that.syncStorage(id)
        } else {
            wx.removeStorageSync('outStockInfo')
            wx.removeStorageSync('outStockData')
        }
        await this.main()
    },
    // 刷新物料信息
    onShow: async function () {
        const that = this
        const outStockData = wx.getStorageSync('outStockData')
        that.setData({
            total: api.total(outStockData, ['price', 'num']),
            count: api.count(outStockData),
            detail: outStockData
        })
    },
    onDisplay() {
        this.setData({
            calendar: true
        })
    },
    // 关闭
    onClose: function (e) {
        let obj = e.currentTarget.dataset
        if (obj) {
            this.setData({[obj.target]: false})
        }
    },
    goto(e) {
        return api.goto(e)
    },
    funRouting(type, detail) {
        switch (type) {
            case 'type':
                return detail.value
            case 'recordDate':
                return api.formatDate(detail)
            case 'sourceTargetName':
                const array = detail.value
                this.setData({
                    ['info.targetId']: array['value']
                })
                return array['text']
        }
    },
    // 确认选择
    onConfirm: function (e) {
        let obj = e.currentTarget.dataset
        let prefix = obj.prefix ? obj.prefix : 'info'
        let field = (prefix ? prefix + '.' : '') + obj.field
        this.setData({
            [obj.target]: false,
            [field]: this.funRouting(obj.field, e.detail),
        })
    },
    showPopup: function (obj) {
      let popup = obj.currentTarget.dataset.show
      if (popup) {
          this.setData({
              [popup]: true
          });
      }
    },
    onDelete(obj) {
        const id = obj.currentTarget.dataset.id
        const {position, instance} = obj.detail;
        const outStockData = wx.getStorageSync('outStockData')
        switch (position) {
            // case 'left':
            // case 'cell':
            //     instance.close();
            //     break;
            case 'right':
                wx.showModal({
                    title: '提示',
                    content: '确定删除吗？',
                    success: (res) => {
                        if (res.confirm) {
                            delete outStockData[id]
                            wx.setStorageSync('outStockData', outStockData)
                            instance.close()
                            this.setData({detail: outStockData})
                            api.showToast('删除成功')
                        }
                    },
                })
                break;
        }
    },
    formSubmit: async function (e) {
        let obj = e.detail.value
        let outStockData = wx.getStorageSync('outStockData')
        let outStockDataArray = Object.values(outStockData)
        outStockDataArray.forEach(function (item) {
            item['id'] = item['id'] ? item['id'] : api.suiji()
            item['mxText'] = item['text']
            delete item['text']
        })
        obj['detail'] = api.arrayToJson(outStockDataArray)
        if (obj) {
            let res = await api.curlPost('?cla=outStock&fun=edit', obj)
            if (res.code === 1) {
                wx.navigateTo({
                    url: 'detailOther?id=' + res.data
                })
                wx.removeStorageSync('outStockData')
                api.showToast('新增成功', 'success')
            } else {
                api.showToast(res.warn)
            }
        }
    }
});